package com.itheima.mp.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
//方式三  与final结合使用，定义一个常量，那么必须在初始化的过程中，为其赋值，被spring注入
@RequiredArgsConstructor
public class UserController {

    //方式一
//    @Autowired
    private final IUserService userService;

    //方式二
//    public UserController(IUserService userService) {
//        this.userService = userService;
//    }

    @ApiOperation("新增用户接口")
    @PostMapping("/user")
    public void saveUser(@RequestBody UserFormDTO userDTO){

        //1.将DTO拷贝到PO
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        //2.新增
        userService.save(user);
    }

    @ApiOperation("删除用户接口")
    @DeleteMapping("/{id}")
    public void deleteUserById(@ApiParam("用户id") @PathVariable("id")long id){
        userService.removeById(id);
    }

    @ApiOperation("根据id查询用户接口")
    @GetMapping("/{id}")
    public UserVO queryUserById(@ApiParam("用户id") @PathVariable("id")long id){
        User user = userService.getById(id);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user,userVO);
        return userVO;
    }

    @ApiOperation("根据id查询用户和用户的所有收货地址接口")
    @GetMapping("/all/{id}")
    public UserVO queryUserAndAddressById(@ApiParam("用户id") @PathVariable("id")long id){

        return userService.getUserAndAddressById(id);
    }

    @ApiOperation("根据id批量查询用户接口")
    @GetMapping()
    public List<UserVO> queryUserByIds(@ApiParam("用户id") @RequestParam("ids") List<Long> ids){
        
        //1.查询po
        List<User> users = userService.listByIds(ids);
        //2.把PO拷贝到VO

        return BeanUtil.copyToList(users, UserVO.class);
    }

    @ApiOperation("根据id批量查询用户和用户的所有收货地址接口")
    @GetMapping("/a")
    public List<UserVO> queryUserAndAdressByIds(@ApiParam("用户id") @RequestParam("ids") List<Long> ids){
        return userService.queryBatchUsersAndAdressByIds(ids);
    }

    @ApiOperation("根据用户id扣减金额")
    @PutMapping("/{id}/deduction/{moeny}")
    public void updateUserMoneyById(@ApiParam("用户id")@PathVariable("id")long id,
                                    @ApiParam("扣减金额")@PathVariable("moeny") Integer money){
        userService.updateuserMoneyById(id,money);
    }

    @ApiOperation("根据复杂条件查询用户接口")
    @GetMapping("/list")
    public List<UserVO> queryUsers(UserQuery userQuery){

        //1.查询po
        List<User> users = userService.queryUsers(userQuery.getName(),userQuery.getStatus(),userQuery.getMinBalance(),userQuery.getMaxBalance());
        //2.把PO拷贝到VO
        return  BeanUtil.copyToList(users,UserVO.class);
    }

    @ApiOperation("通过用户id获取用户地址")
    @GetMapping("/Getaddress/{id}")
    public UserVO queryAddressById(@ApiParam("用户id")@PathVariable("id")Long id){
        return userService.getAddressById(id);
    }

    @ApiOperation("根据复杂条件分页查询用户接口")
    @GetMapping("/page")
    public PageDTO<UserVO> PageQuery(UserQuery userQuery){
        return userService.PageQuerysers(userQuery);
    }
}
